1 package org.apache.maven.surefire.junitcore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.surefire.report.CategorizedReportEntry;
23 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
24 import org.apache.maven.surefire.report.ConsoleOutputReceiverForCurrentThread;
25 import org.apache.maven.surefire.report.ReportEntry;
26 import org.apache.maven.surefire.report.RunListener;
27
28 import java.util.concurrent.atomic.AtomicReference;
29
30
31
32
33
34
35
36 class TestMethod
37 implements ConsoleOutputReceiver
38 {
39 private static final InheritableThreadLocal<TestMethod> TEST_METHOD = new InheritableThreadLocal<TestMethod>();
40
41 private final AtomicReference<LogicalStream> output = new AtomicReference<LogicalStream>();
42
43 private final ReportEntry description;
44
45 private final TestSet testSet;
46
47 private final long startTime;
48
49 private volatile long endTime;
50
51 private volatile ReportEntry testFailure;
52
53 private volatile ReportEntry testError;
54
55 private volatile ReportEntry ignored;
56
57 TestMethod( ReportEntry description, TestSet testSet )
58 {
59 this.description = description;
60 this.testSet = testSet;
61 startTime = System.currentTimeMillis();
62 }
63
64 void testFinished()
65 {
66 setEndTime();
67 }
68
69 void testIgnored( ReportEntry description )
70 {
71 ignored = description;
72 setEndTime();
73 }
74
75 void testFailure( ReportEntry failure )
76 {
77 this.testFailure = failure;
78 setEndTime();
79 }
80
81 void testError( ReportEntry failure )
82 {
83 this.testError = failure;
84 setEndTime();
85 }
86
87 private void setEndTime()
88 {
89 this.endTime = System.currentTimeMillis();
90 }
91
92 int getElapsed()
93 {
94 return endTime > 0 ? (int) ( endTime - startTime ) : 0;
95 }
96
97 long getStartTime()
98 {
99 return startTime;
100 }
101
102 long getEndTime()
103 {
104 return endTime;
105 }
106
107 void replay( RunListener reporter )
108 {
109 if ( ignored != null )
110 {
111 reporter.testSkipped( createReportEntry( ignored ) );
112 }
113 else
114 {
115 ReportEntry descriptionReport = createReportEntry( description );
116 reporter.testStarting( descriptionReport );
117 LogicalStream ls = output.get();
118 if ( ls != null )
119 {
120 ls.writeDetails( (ConsoleOutputReceiver) reporter );
121 }
122
123 if ( testFailure != null )
124 {
125 reporter.testFailed( createReportEntry( testFailure ) );
126 }
127 else if ( testError != null )
128 {
129 reporter.testError( createReportEntry( testError ) );
130 }
131 else
132 {
133 reporter.testSucceeded( descriptionReport );
134 }
135 }
136 }
137
138 private ReportEntry createReportEntry( ReportEntry reportEntry )
139 {
140 return new CategorizedReportEntry( reportEntry.getSourceName(), reportEntry.getName(), reportEntry.getGroup(),
141 reportEntry.getStackTraceWriter(), getElapsed(), reportEntry.getMessage() );
142 }
143
144 void attachToThread()
145 {
146 TEST_METHOD.set( this );
147 ConsoleOutputReceiverForCurrentThread.set( this );
148 }
149
150 void detachFromCurrentThread()
151 {
152 TEST_METHOD.remove();
153 ConsoleOutputReceiverForCurrentThread.remove();
154 }
155
156 static TestMethod getThreadTestMethod()
157 {
158 return TEST_METHOD.get();
159 }
160
161 LogicalStream getLogicalStream()
162 {
163 LogicalStream ls = output.get();
164 if ( ls == null )
165 {
166 ls = new LogicalStream();
167 if ( !output.compareAndSet( null, ls ) )
168 {
169 ls = output.get();
170 }
171 }
172 return ls;
173 }
174
175 public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
176 {
177 getLogicalStream().write( stdout, buf, off, len );
178 }
179
180 public TestSet getTestSet()
181 {
182 return testSet;
183 }
184 }